/* 
 * File:   Files.cpp
 * Copyright (C) 2008, 2009
 *		Sergio Padrino Recio (PadrinatoR)
 *
 * Created on 22 de septiembre de 2008, 20:08
 * This file is part of BombProject.
 * 
 * BombProject is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
   
 * BombProject is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with BombProject.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Files.h"

#include<stdio.h>
#include<sys/stat.h>
#include<dirent.h>
#include<stdlib.h>
#include<errno.h>

/**
 * Returns a file list from a given path, and filtered using the given extension (without the '.').
 * @param path Path to be scanned.
 * @param extension Extension to filter the file list.
 * @return List with the name of the files included in the given path.
 */
vector<string> getFileList(string path, string extension){
	vector<string> list;
	int encontrado=0;
	struct dirent *dp;
	struct stat buf_archivo;
	DIR *dirp;
	string full_path = "", filename = "";
	/* Se abre el directorio de inicio de búsqueda */
	if((dirp=opendir(path.c_str()))==NULL)
	{
		fprintf(stderr, "getFileList - Path %s couldn't be opened\n", path.c_str());
		return list;
	}
	
	/* Se leen las dos primeras entradas . y .. */
	/* Mientras en el directorio haya entradas que leer. */
	while((dp=readdir(dirp))!=NULL)
	{
		/* Se forma el camino completo del archivo. */
		filename = dp->d_name;
		full_path = path + "/" + filename;
		/* Se obtiene información sobre tipo de archivo */
		if((stat(full_path.c_str(),&buf_archivo)) != -1)
		{
			/* Si el archivo es directorio, llamar a busqueda */
			if((buf_archivo.st_mode & S_IFMT) == S_IFDIR) continue;

			if(extension == "" || filename.rfind("." + extension) == filename.length() - 1 - extension.length())
				list.push_back(filename);			
		}else if(errno!=ENOENT){
			fprintf(stderr, "getFileList - Calling 'stat' with file '%s' failed\n", full_path.c_str());
			closedir(dirp);
			exit(-1);
		}
	}
	closedir(dirp);

	return list;
}

/**
 * Returns a directory list from a given path, excluding '.' and '..'.
 * @param path Path to be scanned.
 * @return List with the name of the directories included in the given path.
 */
vector<string> getDirList(string path){
	vector<string> list;
	int encontrado=0;
	struct dirent *dp;
	struct stat buf_archivo;
	DIR *dirp;
	string full_path = "", filename = "";
	/* Se abre el directorio de inicio de búsqueda */
	if((dirp=opendir(path.c_str()))==NULL)
	{
		fprintf(stderr, "getDirList - Path %s couldn't be opened\n", path.c_str());
		return list;
	}
	
	/* Se leen las dos primeras entradas . y .. */
	/* Mientras en el directorio haya entradas que leer. */
	while((dp=readdir(dirp))!=NULL)
	{
		/* Se forma el camino completo del archivo. */
		filename = dp->d_name;
		
		if(filename == "." || filename == "..") continue;
		
		full_path = path + "/" + filename;
		/* Se obtiene información sobre tipo de archivo */
		if((stat(full_path.c_str(),&buf_archivo)) != -1)
		{
			/* Si el archivo es directorio, llamar a busqueda */
			if((buf_archivo.st_mode & S_IFMT) != S_IFDIR) continue;
			
			list.push_back(filename);			
		}else if(errno!=ENOENT){
			fprintf(stderr, "getDirList - Calling 'stat' with file '%s' failed\n", full_path.c_str());
			closedir(dirp);
			exit(-1);
		}
	}
	closedir(dirp);

	return list;
}
